package com.fenghongzhang.day018;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import java.util.ArrayList;
import java.util.List;

public class MusicUtil {


    public  static final  int SINGLE = 0;
    public  static final  int ORDER = 1;
    public  static final  int RANDOM = 2;

    //读取音乐
    public static List<Song> getMusic(Context context){
        List<Song> list = new ArrayList<>();

        ContentResolver contentResolver = context.getContentResolver();

        Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if (query != null){
            while (query.moveToNext()){
                String title = query.getString(query.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String duration = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String data = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DATA));
                String size = query.getString(query.getColumnIndex(MediaStore.Audio.Media.SIZE));

                //过滤短音频
                if(Integer.parseInt(duration) > 1000 * 60){
                    Song song  = new Song(title,artist,duration,data,size,null);
                    list.add(song);
                }

            }


            query.close();
        }
        return list;
    }


    public static String formatTime(String time){
        int i = Integer.parseInt(time);
        if(i / 1000 % 60 < 10){
            return i / 1000 / 60 +":0"+i / 1000 % 60;
        }else{
            return i / 1000 / 60 +":"+i / 1000 % 60;
        }
    }
}
